package edu.tjut.tjutcommunity.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.tjut.tjutcommunity.entity.Kb;
import edu.tjut.tjutcommunity.entity.Kbxx;
import edu.tjut.tjutcommunity.entity.KcbxxQueryInfo;
import edu.tjut.tjutcommunity.entity.User;
import edu.tjut.tjutcommunity.mapper.KbMapper;
import edu.tjut.tjutcommunity.service.KbService;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yhh
 * @since 2022-05-09
 */
@Service
public class KbServiceImpl extends ServiceImpl<KbMapper, Kb> implements KbService {

    final
    KbMapper kbMapper;

    public KbServiceImpl(KbMapper kbMapper) {
        this.kbMapper = kbMapper;
    }

    @Override
    public String getKbJsonByUidAndSemesterAndYear(Integer uid, KcbxxQueryInfo kcbxxQueryInfo) {
        QueryWrapper<Kb> qw = new QueryWrapper<>();
        qw.eq("uid", uid);
        qw.eq("year", kcbxxQueryInfo.getYear());
        qw.eq("semester", kcbxxQueryInfo.getSemester());
        Kb kb = kbMapper.selectOne(qw);
        if (kb == null) return null;
        return kb.getKbxx();
    }

    @Override
    public boolean addKbxx(Kbxx kbxx, User user) {
        Kb kb = new Kb();
        kb.setUid(user.getUid());
        kb.setYear(kbxx.getYear());
        kb.setSemester(kbxx.getSemester());
        kb.setKbxx(JSON.toJSONString(kbxx));
        return kbMapper.insert(kb) == 1;
    }
}
